package com.sl.carcode.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sl.carcode.R;
import com.sl.carcode.bean.GoodInfo;


import java.util.ArrayList;


/**
 * Created by  raysh  on 2017/12/26.
 */

public class GoodLVAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<GoodInfo> list = new ArrayList<>();
    private int kind = 0;

    public GoodLVAdapter(Context context, ArrayList<GoodInfo> arrayList) {
        this.context = context;
        list = arrayList;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View contentView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (null == contentView) {
            viewHolder = new ViewHolder();
            contentView = View.inflate(context, R.layout.item_good, null);
            viewHolder.iv_icon = contentView.findViewById(R.id.icon);
            viewHolder.iv_coupon = contentView.findViewById(R.id.iv_coupon);
            viewHolder.tv_coupon = contentView.findViewById(R.id.tv_coupon);
            viewHolder.tv_sail = contentView.findViewById(R.id.tv_sail);
            viewHolder.tv = contentView.findViewById(R.id.tv);
            contentView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) contentView.getTag();
        }
        if (list.size() > 0) {
            viewHolder.tv_coupon.setText("券后价：" + list.get(i).getPrice());
            viewHolder.tv_sail.setText("正常价：" + list.get(i).getOrg_Price());
            viewHolder.tv.setText("有惠券：" + list.get(i).getQuan_price());
            String url = list.get(i).getPic();
            String quanLink = list.get(i).getQuan_link();
            Log.i("sl", quanLink);
            if (url.contains("\\")) {
                url.replace("\\", "");
            }
            if (quanLink.contains("\\")) {
                quanLink.replace("\\", "");
            }
            Glide.with(context).load(url).into(viewHolder.iv_icon);
            Glide.with(context).load(quanLink).into(viewHolder.iv_coupon);
        }

        return contentView;
    }

    class ViewHolder {
        public ImageView iv_icon;
        public TextView tv_coupon, tv_sail, tv;
        public ImageView iv_coupon;

    }
}
